package com.zdd.ehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdd.ehe.entity.Permission;
import com.zdd.ehe.entity.Role;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.IPermissionService;
import com.zdd.ehe.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hdfs.web.HsftpFileSystem;
import org.mortbay.util.ajax.JSON;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhuiZhu
 * @since 2023-03-13
 */
@Api(tags="权限管理模块")
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
    @Resource
    private IPermissionService permissionService;

    @ApiOperation("分页获取权限接口")
    @PostMapping("/getallpermissionbypage")
    public Result getAllPermissionByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        Page<Permission> pagex = new Page<>(page,pageSize);
        Page<Permission> pageResult = permissionService.getAllPermissionByPage(pagex);
        if(pageResult.getSize() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pageResult);
    }


    @ApiOperation("通过permissionname获取权限列表接口")
    @GetMapping("/getpermissionbypermissionname")
    public Result getPermissionByPerssionName(@RequestParam("permissionName") String permissionName){

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name",permissionName);
        List<Permission> permissionList = permissionService.list(queryWrapper);
        if(permissionList.size() != 0){
            Map<String,Object> data = new HashMap<>();
            data.put("permissionList",permissionList);
            data.put("total",permissionList.size());
            data.put("size",10);
            data.put("current",1);
            data.put("pages",1);
            return Result.success("查询成功",data);
        }else {
            return Result.error(4001,"未查询到该相关权限");
        }

    }

    @ApiOperation("根据模块获取权限列表接口")
    @GetMapping("/getPermissionByModule")
    public Result getPermissionByModule(){
        List<Map<String,Object>> permissionsList = new ArrayList<>();
        List<String> moduleList = Arrays.asList(new String[]{"用户管理", "权限管理","角色管理","用户角色管理","角色权限管理","病虫害信息管理","损失数据管理","产量数据管理","新闻数据管理","价格数据管理","爬虫管理"});
        String result = "";
        for(String module:moduleList){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("module",module);
            List<Permission> permissions = permissionService.list(queryWrapper);
            Map<String,Object> map = new HashMap<>();
            map.put("permissions",permissions);
            map.put("module",module);
            permissionsList.add(map);



            String str = "{ id: -1, label: \'" + module + "\', children: [ ";
            for(Permission permission:permissions){
                String per = "{" +
                        "id: " + permission.getId()  + ", "+
                        "label:" + "'" +permission.getName()+ "'" +
                        "},";
                str += per;
            }
            str += "]},";
            //log.info("str: " + str);
            result += str;

        }

        return Result.success("查询成功","[ " + result.substring(0,result.length()-1) + " ]");
    }


    @ApiOperation("删除角色接口")
    @PostMapping("/deletepermissionbyid")
    public Result deletePermissionById(@RequestParam("id") Integer id){
        Permission permission = permissionService.getById(id);
        if(permission == null){
            return Result.error(4001,"权限编号错误");
        }
        permission.setStatus("0");
        boolean result = permissionService.updateById(permission);
        if(result == false){
            return Result.error(4002,"删除失败");
        }
        return Result.success("删除成功");
    }


    @ApiOperation("重启权限接口")
    @PostMapping("/restartpermissionbyid")
    public Result restartPermissionById(@RequestParam("id") int id){
        Permission permission = permissionService.getById(id);
        if(permission == null){
            return Result.error(4001,"权限编号错误");
        }
        permission.setStatus("1");
        boolean result = permissionService.updateById(permission);
        if(result == false){
            return Result.error(4002,"重启失败");
        }
        return Result.success("重启成功");
    }

    @ApiOperation("修改权限信息接口")
    @PostMapping("/updatepermission")
    public Result updatePermission(@RequestBody Permission permission){
        boolean permission_update = permissionService.updateById(permission);
        if(permission_update == false){
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }


    @ApiOperation("添加权限信息接口")
    @PostMapping("/addpermission")
    public Result addPermission(@RequestBody Permission permission){
        int result = permissionService.addPermission(permission);
        if(result == 1){
            return Result.success("添加成功");
        }else {
            return Result.error(4001,"添加失败");
        }
    }



}
